#!/bin/sh

#
# Copyright (c) 2005 Josh Parsons <jbparsons@ucdavis.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

###
### this init script ensures that the /dev/pts filesystem is working
### it should do nothing if it already is.
###

upgrade_dev() {
    DEV=$1
    if [ ! -e $DEV/ptmx ]; then
	mknod $DEV/ptmx c 5 2
	chown root:root $DEV/ptmx
	chmod 0666 $DEV/ptmx
    fi
    if [ ! -e $DEV/pts ]; then
	mkdir $DEV/pts
	chown root:root $DEV/pts
	chmod 0755 $DEV/pts
    fi    
}

fixperms() {
    if [ -c $DEV/ptmx ]; then
	chmod 0666 $DEV/ptmx
    fi
    if [ -d $DEV/pts ]; then
	chmod 0755 $DEV/pts
    fi
}

mount_devpts() {
    DEV=$1
    upgrade_dev $DEV
    fixperms $DEV
    if ! mount |grep -q "^none on $DEV/pts"; then
	mount -t devpts -o rw,gid=5,mode=620 none $DEV/pts
    fi
}

if [ -d /dev.state ] 
then upgrade_dev /dev.state
fi
mount_devpts /dev
